import { scene } from './utils/init'
import * as THREE from 'three'
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'

function initBase() {
  // 导入加载器加载模型: json格式(.gltf) 或者是二进制(.glb)
  new GLTFLoader().load('model/ferrari.glb', (gltf) => {
    console.log(gltf, 'gltf');
    // 从模型中取出对应的物体
    const model = gltf.scene
    model.traverse(obj => {
      if (obj.name === 'Object_45') {
        obj.material.color = new THREE.Color(0xff00ff)
      }
    })
    // 将物体添加到场景中
    scene.add(model)
  })
}

function createLight () {
    const light = new THREE.AmbientLight(0xffffff)
    scene.add(light)
    // 高级材质，用环境光无法照亮，要用其他光源，为什么？环境光只能照亮普通的材质，高级的材质不反环境光，不反，没有方向的光
    // 高级材质的表面的属性更加的复杂
    const direction = new THREE.DirectionalLight(0xffffff, 2)
    direction.position.set(2, 3, 2)
    scene.add(direction)
    const helper = new THREE.DirectionalLightHelper(direction, .5)
    scene.add(helper)
}

initBase()
createLight()